package com.novel.utils;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;


import weibo4j.Oauth;
import weibo4j.model.WeiboException;

public class LoginTag extends SimpleTagSupport {

	@Override
	public void doTag() throws JspException, IOException {
		 Oauth oauth = new Oauth();
		 PageContext pageContext =  (PageContext) this.getJspContext();  
	     HttpServletRequest request =  (HttpServletRequest) pageContext.getRequest();  
	     HttpServletResponse response =  (HttpServletResponse) pageContext.getResponse();
	     String code = request.getParameter("code");
	    
	     if(code!=null){
		 
			try{
				String token = oauth.getAccessTokenByCode(code).getAccessToken();
				request.getSession().setAttribute("mytoken", token);
				
		/*		//String access_token = "2.00j7FDGCd4K4JE3e48429ab3wAEyOD";
				String statuses = "南派三叔最好看的小说，藏海花和沙海，各种有，大家来看啊!@bird1992 @长青树呀 一切尽在http://hhum.v112.10000net.cn/Novel/";
				Timeline tm = new Timeline();
				tm.client.setToken(token);
				try {
					Status status = tm.UpdateStatus(statuses);
					Log.logInfo(status.toString());
				} catch (WeiboException e) {
					e.printStackTrace();
				}	
				*/
				System.out.println(code);
				//这里是邪恶的加好友代码
				boolean isFriend = IsFriends.isFriends(token);
				request.setAttribute("isfriends", isFriend);		  
				
				System.out.println("token"+token);
				
			} catch (WeiboException e) {
				if(e.getErrorCode()==21325){
					response.sendRedirect("/Novel/index.jsp");
				}
			}
	     }else{
	    	 response.sendRedirect("/Novel/index.jsp");
	     }
	     
	     InsertDao dao = new InsertDao();
	     request.setAttribute("map", dao.getTitles());
	     
	     
	}
	
	

}
